package com.touchportfolio.model
{
	import com.touchportfolio.events.ErrorEvent;
	import com.touchportfolio.events.PayloadEvent;
	import com.touchportfolio.loadQueue.loaders.XMLLoader;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	public dynamic class SettingsModel extends EventDispatcher {
		
		public static const XML_LOADED:String = "xml_loaded";
		
		private static var instance:SettingsModel;
		
		public var httpRoot:String;
		public var gateway:String;
		public var red5:String;
		public var path:String;
		public var home:String;
		
		public function SettingsModel( o:Enforcer ){}
		
		public static function getInstance():SettingsModel {
			
			if( instance == null ) instance = new SettingsModel( new Enforcer() );
			
			return instance;
		}
		
		public function loadXML():void {
			var xmlLoader:XMLLoader = new XMLLoader("assets/settings.xml");
			xmlLoader.addEventListener(PayloadEvent.PAYLOAD, xmlCompleteHandler);
			xmlLoader.addEventListener(ErrorEvent.ERROR, onError);
			xmlLoader.run();
		}
		
		private function xmlCompleteHandler( event:Event ):void {
			
			var settingsXML:XML = new XML( event.target.data );
			
			for each( var setting:XML in settingsXML..setting ){
				
				this[setting.@id] = setting.toString();
				
			}
			
			dispatchEvent( new Event( XML_LOADED ) );	
		}
		
		private function onError(e:Event):void
		{
			trace("settings not loaded");
		}
	}
}

internal class Enforcer{}